<?php

class User
{
	private $table = "sys_user";
	private $parameters = Array();
	
	private $id;
	private $name;
	private $login;
	private $pass;	
	private $active;
	private $id_group;
	private $dt_register;
	
	public function set($name, $login, $pass, $active, $dt_register = null, $id_group = 0, $id = 0)
	{
		$this->id = $id;
		$this->name = $name;
		$this->login = $login;
		$this->pass = $pass;
		$this->active = $active;
		$this->id_group = $id_group;
		$this->dt_register = $dt_register;
		
		
		//deve ser montado na ordem que foi criado a tabela
		array_push($this->parameters, "'".$this->name."'");
		array_push($this->parameters, "'".$this->login."'");
		array_push($this->parameters, "'".$this->pass."'");
		array_push($this->parameters, $this->active);
		array_push($this->parameters, $this->id_group);
		array_push($this->parameters, $this->dt_register);
	}
	
	public function get($value)
	{
		return $this->$value;
				
	}
	
	public function insert() 
	{
		$command = new DbCommand();
		
		$command->insertCommand($this->table, implode(",", $this->parameters));
	}
	
	public function update()
	{
		$command = new DbCommand();
		
		$query = $command->ExecCommand("SELECT * FROM ".$this->table);
		
		$i = 0;
		
		while($field = mysql_fetch_field($query))
		{	
			if($i > 0){
				$this->parameters[$i-1] = $field->name." = ".$this->parameters[$i-1];
			}
			
			$i++;
		}	
		$command->updateCommand($this->table, implode(",", $this->parameters), $this->id);
	}
	
	public function select($id)
	{
		$command = new DbCommand();
		$result = $command->selectCommand($this->table, $id);
		
		$value = mysql_fetch_object($result);
		$this->set(
			$value->name,
			$value->login,
			$value->pass,
			$value->active,
			$value->dt_register,
			$value->id_group,
			$value->id
		);
	}
	
	public function search($options = "*", $method = "", $parameters){
		$command = new DbCommand();
		return $command->searchCommand($options, $this->table, $method, $parameters);
	}
	
	
	public function delete($id){
		$command = new DbCommand();
		$command->deleteCommand($this->table, $id);
	}
	
	public function listAll($fields = "*", $options = "")
	{
		$command = new DbCommand();
		return $command->listCommand($this->table, $fields, $options);
	}
}